package amap

import (
	"errors"
	"fmt"
	"hft/Http/Services/front/amap/amap_model"
	"hft/Http/Services/front/amap/amap_utils"
	"hft/e"
	maptool "hft/tools/map_tool"
	"net/url"
)

type Amap struct{}

// 周边搜索
func (*Amap) SearchAround(data *amap_model.SearchAround, ip string) (result *amap_model.Res, err error) {
	fmt.Println(ip)
	if data.Radius == "" {
		data.Radius = "5000"
	}
	if data.SortRule == "" {
		data.SortRule = "distance"
	}
	if data.CityLimit == "" {
		data.CityLimit = "false"
	}
	if data.PageSize == "" {
		data.PageSize = "10"
	}
	if data.PageNum == "" {
		data.PageNum = "1"
	}
	if data.OutPut == "" {
		data.OutPut = "json"
	}
	var regionName = ""
	if data.Region == "" {
		regionName, _, err = amap_utils.GetIP(ip)
		if err != nil {
			return nil, err
		}
		data.Region = regionName
	} else {
		regionName = data.Region
	}
	var params = &url.Values{
		"keywords":   []string{data.Keywords},
		"types":      []string{data.Types},
		"location":   []string{data.Location},
		"radius":     []string{data.Region},
		"sortrule":   []string{data.SortRule},
		"region":     []string{regionName},
		"city_limit": []string{data.CityLimit},
		"show_field": []string{data.ShowField},
		"page_size":  []string{data.PageSize},
		"page_num":   []string{data.PageNum},
		"output":     []string{data.OutPut},
	}
	res, err := maptool.NewAmapMapApi("/v5/place/around", params)
	if err != nil {
		return nil, errors.New(e.ErrorInfo)
	}
	var newRes = &amap_model.Res{
		RegionName: regionName,
		Data:       string(res),
	}
	return newRes, nil
}

func (*Amap) GetIp(ip string) (string, string, error) {
	city, location, err := amap_utils.GetIP(ip)
	if err != nil {
		return "", "", errors.New(e.ErrorInfo)
	}
	return city, location, nil
}

// 关键字
func (*Amap) KeyWordSearch(data *amap_model.KeyWordSearch, ip string) (result *amap_model.Res, err error) {
	fmt.Println(ip)
	if data.CityLimit == "" {
		data.CityLimit = "true"
	}
	if data.PageSize == "" {
		data.PageSize = "10"
	}
	if data.PageNum == "" {
		data.PageNum = "1"
	}
	if data.OutPut == "" {
		data.OutPut = "json"
	}
	var regionName = ""
	if data.Region == "" {
		regionName, _, err = amap_utils.GetIP(ip)
		if err != nil {
			return nil, err
		}
		fmt.Println(regionName, "ajsdjasjdkask")
		data.Region = regionName
	} else {
		regionName = data.Region
	}
	fmt.Println(data.Region == "", "地点:", regionName, data.Region)
	var params = &url.Values{
		"keywords":   []string{data.Keywords},
		"types":      []string{data.Types},
		"radius":     []string{data.Region},
		"region":     []string{regionName},
		"city_limit": []string{data.CityLimit},
		"show_field": []string{data.ShowField},
		"page_size":  []string{data.PageSize},
		"page_num":   []string{data.PageNum},
		"output":     []string{data.OutPut},
	}
	res, err := maptool.NewAmapMapApi("/v5/place/text", params)
	if err != nil {
		return nil, errors.New(e.ErrorInfo)
	}
	var newRes = &amap_model.Res{
		RegionName: regionName,
		Data:       string(res),
	}
	return newRes, nil
}

var AmapService = new(Amap)
